package kr.co.redbean.store
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	public class ObjectStore extends EventDispatcher
	{
		static public var _singleton:ObjectStore;
		private var _objectProxyList:Vector.<ObjectInfo>;
		private var _adStore:ADStore;
		
		public function ObjectStore(singletoneForce:SingletonForce)
		{
			super();
			_adStore = new ADStore;
		}
		
		static public function getInstance():ObjectStore {
			if(_singleton) {}
			else {
				_singleton = new ObjectStore(new SingletonForce);
			}
			return _singleton;
		}
		
		public function initADs():void { trace('init ads');
			var loader:Loader;
			
			loader = new Loader;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, hnAD1Loaded);
			loader.load(new URLRequest('kr/co/redbean/store/MenuAD.swf'), new LoaderContext( false , _adStore.menuAD ));
			
			function hnAD1Loaded($e:Event):void { trace('ad1 ok');
				$e.target.removeEventListener(Event.COMPLETE, hnAD1Loaded);
				$e.target.addEventListener(Event.COMPLETE, hnAD2Loaded);
				
				loader.load(new URLRequest('kr/co/redbean/store/MemberAD.swf'), new LoaderContext( false, _adStore.memberAD));
			}
			function hnAD2Loaded($e:Event):void { trace('ad2 ok');
				$e.target.removeEventListener(Event.COMPLETE, hnAD2Loaded);
				$e.target.addEventListener(Event.COMPLETE, hnAD3Loaded);
				
				loader.load(new URLRequest('kr/co/redbean/store/PopupAD.swf'), new LoaderContext( false, _adStore.popupAD));
			}
			function hnAD3Loaded($e:Event):void { trace('ad3 ok');
				$e.target.removeEventListener(Event.COMPLETE, hnAD3Loaded);
				$e.target.addEventListener(Event.COMPLETE, hnAD4Loaded);
				
				loader.load(new URLRequest('kr/co/redbean/store/AssetsAD.swf'), new LoaderContext( false, _adStore.assetsAD));
			}
			function hnAD4Loaded($e:Event):void { trace('ad4 ok');
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		public function setObjectProxyList(obj:XML):void { trace('setting module list');
			var list:XMLList;
			var len:uint;
			list = obj..module;
			len = list.length();
			
			_objectProxyList = new Vector.<ObjectInfo>(len,true);
			
			for(var i:uint = 0; i<len; i++) { trace(list[i].$name,i);
				_objectProxyList[i] = new ObjectInfo(list[i].@name, list[i].@domain, list[i].@priority, list[i]..properties);
			}
			_objectProxyList.sort(sortObject);
		}
		
		public function getObjectByName(menu:String):IObjectInfo {
			var rtn:IObjectInfo;
			
//			for each(var o:ObjectInfo in _objectProxyList) {
			for(var i:uint = 0, j:uint = _objectProxyList.length; i<j; i++ ) {
				if(String(_objectProxyList[i].name).toLowerCase() === String(menu).toLowerCase()) {
					rtn = _objectProxyList[i];
					break;
				}
			}
			return rtn;
		}
		
		public function get objectProxyList():Vector.<ObjectInfo> {
			return _objectProxyList;
		}

		public function get adStore():ADStore
		{
			return _adStore;
		}

		private function sortObject(x:ObjectInfo, y:ObjectInfo):int { trace(x,y);
			if (x.priority < y.priority)
			{
				return -1;
			}
			else if (x.priority > y.priority)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
	}
}

class SingletonForce {}